

#include "Test.h"

#if TEST_SE
#include <stdint.h>
#include "Debug.h"
#include "Bsp_Delay.h"
#include "Connector.h"
#include "Driver.h"
#include "Slot.h"

void test_CharacterModule(void);


static void testConnector(void){
	connector_t* con = Connector_Get(0);
	Connector_ConfigIO(con, CON_IO_BIT_OUT);
	while(1){
		Connector_SetIOState(con, 1);
		Connector_SetResetState(con, 1);
		BSP_Delay_ms(100);
		Connector_SetIOState(con, 0);
		Connector_SetResetState(con, 0);
		BSP_Delay_ms(100);
	}
}

static void testUsart(void){
	CharacterModule_Init();
}

extern slot_t Slot[1];

static void testPowerOn(void){
	uint8_t atr[32];
	uint8_t alen = 0;
	uint8_t ret;
	CharacterModule_Init();
	Slot_Init();
	while (1){
		memset(atr, 0, sizeof(atr));
		alen = 0;
		ret = Slot_PowerOn(Slot, atr, &alen);
		if(ret){
			DBG_WRITE("Power On fail\r\n");
			Led_Off(LedId_Greent);
		}
		else{
			DBG_WRITE("ATR: "); DBG_PRINT_BUF(atr, alen); DBG_WRITE("\r\n");
			Led_On(LedId_Greent);
		}
		BSP_Delay_ms(100);
//			Slot_PowerOff(Slot);
		BSP_Delay_ms(100);
		CharacterModule_Init();
		Slot_Init();
	}
}

static void testAPDU(void){
	uint8_t atr[32];
	uint8_t alen = 0;
	uint8_t ret;
	uint8_t capdu[] = {0x00, 0x84, 0x00, 0x00, 0x08};
	uint8_t rapdu[16];
	uint32_t rlen;
	
	CharacterModule_Init();
	Slot_Init();
	memset(atr, 0, sizeof(atr));
	alen = 0;
	ret = Slot_PowerOn(Slot, atr, &alen);
	if(ret){
		DBG_WRITE("Power On fail\r\n");
		return;
	}
	while(1){
		BSP_Delay_ms(100);
		ret = Slot_ExchangAPDU(Slot, capdu, sizeof(capdu), rapdu, &rlen);
		if(ret){
			DBG_WRITE("APDU fail\r\n");
		}
		else{
			DBG_WRITE("RAPDU:"); 
			DBG_PRINT_BUF(rapdu, rlen);
			DBG_WRITE("\r\n");
		}
	}
}

void TestSE(void){	
	int ret = 0;
	uint8_t uid[12];
	uint8_t len;
	uint8_t sak;
	
	DBG_WRITE("Test SE:\r\n");

	Connector_Init();
//		testConnector();
//		test_CharacterModule();
	testPowerOn();
	while(1){
		testAPDU();
		BSP_Delay_ms(1000);
	}
}
#endif 


